package com.slipper.system.ai.controller;

import com.slipper.ai.service.ChatService;
import com.slipper.ai.vo.ChatVO;
import com.slipper.core.gson.GsonUtil;
import com.slipper.system.ai.vo.AiApplicationVO;
import com.slipper.util.*;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.slipper.system.ai.bo.AiApplicationBO;
import com.slipper.system.ai.service.AiApplicationService;
import com.slipper.constants.Constants;

import java.nio.charset.StandardCharsets;
import java.util.*;

@RestController
@RequestMapping("/system/aiApplication")
public class AiApplicationController {
    @Resource
    private ChatService chatService;
    @Resource
    private AiApplicationService aiApplicationService;

    @PostMapping(value = "/queryBy")
    public String queryBy(@RequestBody AiApplicationBO bo) {
        if(Tool.isBlank(bo.getId())) {
            return ResultUtil.failureResult("数据获取失败！");
        }
        return ResultUtil.successResult(aiApplicationService.queryBy(bo.getId()));
    }

    @PostMapping(value = "/queryList")
    public String queryList(@RequestBody AiApplicationBO bo) {
        return ResultUtil.successResult(aiApplicationService.queryList(bo));
    }
    
    @PostMapping(value = "/queryPage")
    public String queryPage(@RequestBody AiApplicationBO bo) {
        return ResultUtil.successResult(aiApplicationService.queryPage(bo));
    }

    @PostMapping(value = "/save")
    public String save(@RequestBody AiApplicationBO bo) {
        bo.setOperateId(Constants.getCurrentUser().getEmployeeId());
        return ResultUtil.saveResult(aiApplicationService.save(bo), bo);
    }

    @PostMapping(value = "/delete")
    public String delete(@RequestBody AiApplicationBO bo) {
        return ResultUtil.deleteResult(aiApplicationService.delete(bo));
    }

    @PostMapping(value = "/generatePrompt")
    public String generatePrompt(@RequestBody ChatVO vo) {
        return ResultUtil.successResult("生成成功。", chatService.generatePrompt(vo.getMessage()));
    }

    @PostMapping(value = "/debutChat")
    public String debutChat(@RequestBody AiApplicationBO bo) {
        return chatService.chat(bo, true);
    }

    @PostMapping(value = "/exportData")
    public void exportData(HttpServletResponse response, @RequestBody AiApplicationBO bo) {
        AiApplicationVO vo = new AiApplicationVO();
        vo.setAiApplicationList(aiApplicationService.queryList(bo));
        String fileName = "application-" + DateUtil.format(new Date(), "dd-HH-mm-ss") + ".dat";
        FileUtil.downloadContent(response, fileName, AESUtil.encrypt(GsonUtil.toJson(vo)));
    }

    @PostMapping(value = "/importData")
    public String importData(AiApplicationBO bo) {
        if (bo.getFile() == null) {
            return ResultUtil.failureResult("请选择上传文件！");
        }
        try {
            String content = new String(bo.getFile().getBytes(), StandardCharsets.UTF_8);
            AiApplicationVO vo = GsonUtil.fromJson(AESUtil.decrypt(content), AiApplicationVO.class);
            aiApplicationService.saveList(vo.getAiApplicationList());
            return ResultUtil.successResult("导入供应商模型成功。");
        } catch (Exception e) {
            return ResultUtil.failureResult("文件解释出错！");
        }
    }
}
